package com.loongson;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

public class HttpClientUtil {
    private static final HttpClient httpClient = HttpClients.createDefault();

    public static HttpResponse sendGetRequest(String url, Map<String, String> headers) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        setHeaders(httpGet, headers);

        return httpClient.execute(httpGet);
    }

    public static HttpResponse sendPostRequest(String url, String requestBody, Map<String, String> headers) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        setHeaders(httpPost, headers);

        if (requestBody != null) {
            StringEntity entity = new StringEntity(requestBody);
            httpPost.setEntity(entity);
        }

        return httpClient.execute(httpPost);
    }

    public static HttpResponse sendPutRequest(String url, String requestBody, Map<String, String> headers) throws IOException {
        HttpPut httpPut = new HttpPut(url);
        setHeaders(httpPut, headers);

        if (requestBody != null) {
            StringEntity entity = new StringEntity(requestBody);
            httpPut.setEntity(entity);
        }

        return httpClient.execute(httpPut);
    }

    public static HttpResponse sendDeleteRequest(String url, Map<String, String> headers) throws IOException {
        HttpDelete httpDelete = new HttpDelete(url);
        setHeaders(httpDelete, headers);

        return httpClient.execute(httpDelete);
    }

    private static void setHeaders(HttpRequestBase httpRequest, Map<String, String> headers) {
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpRequest.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    public static int getResponseStatus(HttpResponse response) {
        return response.getStatusLine().getStatusCode();
    }

    public static String getResponseBody(HttpResponse response) throws IOException {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toString(entity);
        }
        return null;
    }

    public static void main(String[] args)throws  IOException{
        String url = "http://127.0.0.1:9080/cloudservice/demo/123456";
        Map<String, String> headers = new HashMap<>();
        headers.put(HttpHeaders.CONTENT_TYPE, "application/json");
        headers.put("TXID", "123456");
        String requestBody = "{\"name\": \"John\", \"age\": 30}";

        try {
            HttpResponse response = HttpClientUtil.sendPostRequest(url, requestBody, headers);
            int statusCode = HttpClientUtil.getResponseStatus(response);
            String responseBody = HttpClientUtil.getResponseBody(response);

            System.out.println("Status code: " + statusCode);
            System.out.println("Response body: " + responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
     }
}